package gui;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import Aplicacion.MP3Player;
import Aplicacion.Variables_globales;
import Entidades.Paises;
import Entidades.Posesion;

import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import data.Database;

import java.awt.Font;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Cursor;

public class PosJugadoresGui implements WindowListener {
	private final Database db = new Database();
	private final int soldados = db.getSoldadosDisponibles();
	private int soldados_pais;
	private JFrame frame;
	private JSpinner contadorSoldados;
	private JLabel paisDestino;
	private int id_pais;
	
	public PosJugadoresGui(int id_pais, String nombre, int x, int y) {
		this.id_pais = id_pais;
		this.soldados_pais = db.getSoldadosDisponiblesEnPais(id_pais);
		this.initialize();
		
		paisDestino.setText(nombre);
		frame.setLocation(x, y);
		
		frame.setVisible(true);
	}

	private void initialize() {
		frame = new JFrame();
		frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		frame.setTitle("Posicionar Soldados");
		frame.setAlwaysOnTop(true);
		frame.setResizable(false);
		frame.setSize(559, 55);
//		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel lblAadir = new JLabel("A\u00F1adir ");
		panel.add(lblAadir);
		
		contadorSoldados = new JSpinner();
		contadorSoldados.setPreferredSize(new Dimension(100, 20));
		try {
			contadorSoldados.setModel(new SpinnerNumberModel(1, 0, soldados, 1));
		} catch (IllegalArgumentException e) {
			frame.dispose();
		}
		panel.add(contadorSoldados);
		
		JLabel lblSoldadosAlPais = new JLabel("Soldados al Pais");
		panel.add(lblSoldadosAlPais);
		
		paisDestino = new JLabel("Europa");
		paisDestino.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		panel.add(paisDestino);
		frame.addWindowListener(this);
		
		contadorSoldados.addChangeListener(new ChangeListener () {
			@Override
			public void stateChanged(ChangeEvent e) {
				int tmp = Integer.parseInt(contadorSoldados.getValue().toString());
				db.updateSoldadosDisponibles(soldados-tmp);
				db.updateSoldadosPais(id_pais, soldados_pais+tmp);
				
				try {
					MP3Player WAV = new MP3Player();
					WAV.playDaemon(this.getClass().getResource("/Imagenes/clickMenu.mp3").toString());
				} catch (IllegalThreadStateException ex) {}
			}
		});
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {
		Variables_globales.PAUSA = false;
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}
}
